<?php
/**
 * Created by PhpStorm.
 * User: 69568
 * Date: 2018/2/7
 * Time: 10:36
 */

namespace App\Http\Controllers\Api;


use App\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Models\User;
use Illuminate\Http\Request;

class TeamController extends Controller
{

    use ApiResponse;

    public function team(Request $request)
    {

        /** @var \App\Models\User $user */
        $user = $request->user('api');
        $level = $request->input('level', 0);
        $query = User::whereParent($user->id);
        if ($level > 0) {
            $query->where('level', $level);
        }
        $users = $query->get();
        return $this->message('获取成功', UserResource::collection($users));
    }

    public function total(Request $request)
    {

        /** @var \App\Models\User $user */
        $user = $request->user('api');
        $level = $request->input('level', 0);
        $total = User::whereParent($user->id)->count();
        $vip = User::whereParent($user->id)->where('level', 1)->count();
        $zy = User::whereParent($user->id)->where('level', 2)->count();
        $fl = User::whereParent($user->id)->where('level', 3)->count();
        $jl = User::whereParent($user->id)->where('level', 4)->count();
        $zj = User::whereParent($user->id)->where('level', 5)->count();
        $area = User::whereParent($user->id)->where('level', 6)->count();
        return $this->message('获取成功', ['total' => $total, 'vip' => $vip, 'fl' => $fl, 'jl' => $jl, 'zj' => $zj, 'area' => $area, 'zy' => $zy]);
    }

}